package com.zadtek.shopmall.base;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.view.SpacesItemDecoration;

/**
 * Created by Zackv on 2018/4/11.
 */

public abstract class AbsListActivity extends AbsActivity implements BaseQuickAdapter.RequestLoadMoreListener {
    private ImageView ivBack;
    private TextView title;
    private RecyclerView recyclerView;
    private BaseQuickAdapter adapter;

    @Override
    public int setView() {
        return R.layout.activity_list;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();


    }


    private void initLogic() {
        adapter = getAdapter();
        adapter.setOnLoadMoreListener(this,recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OnItemClick(position);
            }
        });
        View view = getLayoutInflater().inflate(R.layout.normal_empty_view, (ViewGroup) recyclerView.getParent(),false);
        //ViewGroup.LayoutParams para = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        //view.setLayoutParams(para);
        adapter.setEmptyView(view);
        recyclerView.setAdapter(adapter);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        title.setText(getTopTitle());
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        title = (TextView) findViewById(R.id.title);
        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(getResources().getDimensionPixelSize(R.dimen.little_margin)));
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    protected abstract String getTopTitle();
    public abstract BaseQuickAdapter getAdapter();
    public abstract void loadMore();
    protected abstract void requestData();
    public abstract void OnItemClick(int position);

}
